package server

import (
	"encoding/json"
	"net/http"
	"open-platform/cache"
)

func getToken(w http.ResponseWriter, r *http.Request) {

	appId := r.URL.Query().Get("appid")

	if "" == appId {
		response(w, "参数错误", "400")
		return
	}

	officialAccount, err := cache.GetOfficialAccount(appId)

	if nil != err {
		response(w, err.Error(), "500")
		return
	}

	authorizerToken, err := officialAccount.GetToken()

	if nil != err {
		response(w, err.Error(), "500")
		return
	}

	jsonStr, err := json.Marshal(authorizerToken)

	if nil != err {
		response(w, err.Error(), "500")
		return
	}

	w.Header().Set("Status Code", "200")
	_, _ = w.Write(jsonStr)

}
